// pages/exam-mock/index.js
import {
    request_post
} from '../../utils/request';
import {
    areaList
} from '@vant/area-data';
Page({
    data: {
        examMock: {
            name: '',
            mobile: '',
            addr: '',
            pcdName: '',
            pcdCode: ''
        },
        PCDChooseShow: false,
        areaList,
        nameError: false,
        mobileError: false,
        pcdError: false,
        addrError: false
    },
    onLoad(options) {
        request_post('/exam-mock/get-my').then(res => {
            if (!res) return
            this.setData({
                examMock: res
            })
        })
    },
    onChange(event) {
        this.data.examMock[event.target.dataset.name] = event.detail
        this.setData({
            examMock: this.data.examMock
        })
    },
    togglePCDChoose() {
        this.setData({
            PCDChooseShow: !this.data.PCDChooseShow
        })
    },
    confirmPCDChoose(event, a, b) {
        let value = event.detail.values
        this.togglePCDChoose()
        if (!value) return
        this.data.examMock.pcdName = value.map(item => item.name).reduce((pre, now) => {
            return pre + " " + now
        })

        this.data.examMock.pcdCode = value[value.length - 1].code
        this.setData({
            examMock: this.data.examMock
        })
    },
    confirm() {
        this.setData({
            nameError: this.data.examMock.name.trim().length < 1,
            mobileError: !/^1\d{10}$/.test(this.data.examMock.mobile.trim()),
            pcdError: !this.data.examMock.pcdCode,
            addrError: this.data.examMock.addr.trim().length < 1,
            name: this.data.examMock.name.trim(),
            mobile: this.data.examMock.mobile.trim(),
            addr: this.data.examMock.addr.trim()
        })

        if (this.data.nameError || this.data.mobileError || this.data.pcdError || this.data.addrError) return

        request_post('/exam-mock/upsert', this.data.examMock).then(res => {
            wx.navigateBack({
                delta: 1
            })
        })
    },
})